﻿using CommonServiceLocator;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using Jn698._45GWTool.Services;
using Jn698._45GWTool.ViewModels.Controls.DataControls;
using Jn698._45Tool.Service;
using nblTool.utils;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using WebFirst.Entities;


namespace Jn698._45GWTool.ViewModels
{
    public class EditPlanListViewModel:ViewModelBase
    {
        private ClassIdService classIdService = new ClassIdService();
        private GWCmdMakeUpPlanService GWCmdMakeUpPlanService = new GWCmdMakeUpPlanService();
        public static int point = -1;
        public EditPlanListViewModel() 
        {
            IdList = new List<string>();
            IdList.Add("所有");
            IdList.AddRange(classIdService.Select());
            SelectedID = 0;
        }

        private int selectedID;

        public int SelectedID
        {
            get { return selectedID; }
            set { selectedID = value; RaisePropertyChanged(); UpdateDataGird(); }
        }


        private List<string> idList;

        public List<string> IdList
        {
            get { return idList; }
            set { idList = value; }
        }

        private ObservableCollection<GWCmdMakeUpPlan> objectIdList;

        public ObservableCollection<GWCmdMakeUpPlan> ObjectIdList
        {
            get { return objectIdList; }
            set { objectIdList = value; RaisePropertyChanged(); }
        }

        private int selectedObject;

        public int SelectedObject
        {
            get
            {
                
                return selectedObject;
            }
            set { selectedObject = value; RaisePropertyChanged(); }
        }


        /// <summary>
        /// 刷新ObjectId的列表数据
        /// </summary>
        private void UpdateDataGird()
        {

            //添加数据
            List<GWCmdMakeUpPlan> listobject;
            if (SelectedID == 0)
            {
                listobject = SqlSugarHelper.Db.Queryable<GWCmdMakeUpPlan>().OrderBy(o => o.ObjectIdentifier).ToList();

            }
            else
            {
                listobject = SqlSugarHelper.Db.Queryable<GWCmdMakeUpPlan>().Where(o => o.InterfaceClass == SelectedID).OrderBy(o => o.ObjectIdentifier).ToList();
            }
            
            ObjectIdList = new ObservableCollection<GWCmdMakeUpPlan>(listobject);
        }

        public RelayCommand<Window> OKCommand
        {
            get
            {
                var command = new RelayCommand<Window>((w) =>
                {
                    try
                    {
                        var vm = ServiceLocator.Current.GetInstance<PlanSetViewModel>();
                        var data = objectIdList[SelectedObject];
                        BatchParaConfig batchParaConfig = new BatchParaConfig();
                        batchParaConfig.ObjectName = data.SubItemName;
                        batchParaConfig.ObjectClass = data.InterfaceClass.ToString();
                        batchParaConfig.StrOAD = data.ObjectIdentifier;
                        batchParaConfig.ClientAPDU = data.OpenrateType;
                        if (Convert.ToInt32(data.ObjectIdentifier, 16) < 0x10000000)
                        {
                            batchParaConfig.ReadSecurity = 0;
                            batchParaConfig.WriteSecurity = 0;
                        }
                        else if (Convert.ToInt32(data.ObjectIdentifier, 16) < 0x20000000)
                        {
                            if (data.ObjectIdentifier.Contains("202C"))
                            {
                                batchParaConfig.ReadSecurity = 0;
                                batchParaConfig.WriteSecurity = 0;
                            }
                            else
                            {
                                batchParaConfig.ReadSecurity = 3;
                                batchParaConfig.WriteSecurity = 3;
                            }
                        }
                        else if (Convert.ToInt32(data.ObjectIdentifier, 16) < 0x30000000)
                        {
                            batchParaConfig.ReadSecurity = 3;
                            batchParaConfig.WriteSecurity = 5;
                        }
                        else if (Convert.ToInt32(data.ObjectIdentifier, 16) < 0x40000000)
                        {
                            batchParaConfig.ReadSecurity = 3;
                            batchParaConfig.WriteSecurity = 5;
                        }
                        else if (Convert.ToInt32(data.ObjectIdentifier, 16) < 0x50000000)
                        {
                            if (data.ObjectIdentifier.Contains("4000")|| data.ObjectIdentifier.Contains("4001"))
                            {
                                batchParaConfig.ReadSecurity = 0;
                                batchParaConfig.WriteSecurity = 5;
                            }
                            else if (data.ObjectIdentifier.Contains("4002"))
                            {
                                batchParaConfig.ReadSecurity = 0;
                                batchParaConfig.WriteSecurity = 3;
                            }
                            else if (data.ObjectIdentifier.Contains("4003"))
                            {
                                batchParaConfig.ReadSecurity = 3;
                                batchParaConfig.WriteSecurity = 3;
                            }
                            else if (data.ObjectIdentifier.Contains("400A")
                            || data.ObjectIdentifier.Contains("400B")
                            || data.ObjectIdentifier.Contains("400C")
                            || data.ObjectIdentifier.Contains("400D")
                            || data.ObjectIdentifier.Contains("400E")
                            || data.ObjectIdentifier.Contains("4018")
                            || data.ObjectIdentifier.Contains("4019")
                            || data.ObjectIdentifier.Contains("401A")
                            || data.ObjectIdentifier.Contains("401B"))
                            {
                                batchParaConfig.ReadSecurity = 3;
                                batchParaConfig.WriteSecurity = 5;
                            }
                            else if (data.ObjectIdentifier.Contains("4111"))
                            {
                                batchParaConfig.ReadSecurity = 0;
                                batchParaConfig.WriteSecurity = 5;
                            }
                            else
                            {
                                batchParaConfig.ReadSecurity = 3;
                                batchParaConfig.WriteSecurity = 5;
                            }
                        }
                        else if (Convert.ToInt32(data.ObjectIdentifier, 16) < 0x60000000)
                        {
                            batchParaConfig.ReadSecurity = 3;
                            batchParaConfig.WriteSecurity = 5;
                        }
                        else if (Convert.ToInt32(data.ObjectIdentifier, 16) < 0x70000000)
                        {
                            batchParaConfig.ReadSecurity = 0;
                            batchParaConfig.WriteSecurity = 0;
                        }
                        else if (Convert.ToUInt32(data.ObjectIdentifier, 16) < 0x80000000)
                        {
                            batchParaConfig.ReadSecurity = 0;
                            batchParaConfig.WriteSecurity = 0;
                        }
                        else if (Convert.ToUInt32(data.ObjectIdentifier, 16) < 0xF0000000)
                        {
                            batchParaConfig.ReadSecurity = 3;
                            batchParaConfig.WriteSecurity = 5;
                        }
                        else if (data.ObjectIdentifier.Contains("F000"))
                        {
                            batchParaConfig.ReadSecurity = 0;
                            batchParaConfig.WriteSecurity = 5;
                        }
                        else if (data.ObjectIdentifier.Contains("F001"))
                        {
                            batchParaConfig.ReadSecurity = 0;
                            batchParaConfig.WriteSecurity = 4;
                        }
                        else if (data.ObjectIdentifier.Contains("F002"))
                        {
                            batchParaConfig.ReadSecurity = 0;
                            batchParaConfig.WriteSecurity = 0;
                        }
                        else if (data.ObjectIdentifier.Contains("F100"))
                        {
                            batchParaConfig.ReadSecurity = 0;
                            batchParaConfig.WriteSecurity = 3;
                        }
                        else if (data.ObjectIdentifier.Contains("F101"))
                        {
                            batchParaConfig.ReadSecurity = 0;
                            batchParaConfig.WriteSecurity = 5;
                        }
                        else if (Convert.ToUInt32(data.ObjectIdentifier, 16) < 0xF2FFFFFF&&Convert.ToUInt32(data.ObjectIdentifier, 16) > 0xF2000000)
                        {
                            batchParaConfig.ReadSecurity = 0;
                            batchParaConfig.WriteSecurity = 0;
                        }
                        else if (Convert.ToUInt32(data.ObjectIdentifier, 16) < 0xFFFFFFFF && Convert.ToUInt32(data.ObjectIdentifier, 16) > 0xFF000000)
                        {
                            batchParaConfig.ReadSecurity = 0;
                            batchParaConfig.WriteSecurity = 0;
                        }
                        else
                        {
                            batchParaConfig.ReadSecurity = 3;
                            batchParaConfig.WriteSecurity = 5;
                        }
                        vm.BatchParaConfigList.Insert(point + 1, batchParaConfig);
                        SqlSugarHelper.Db.Ado.BeginTran();
                        SqlSugarHelper.Db.DbMaintenance.TruncateTable<BatchParaConfig>();
                        SqlSugarHelper.Db.Insertable<BatchParaConfig>(vm.BatchParaConfigList.ToList()).UseParameter().ExecuteCommand();
                        SqlSugarHelper.Db.Ado.CommitTran();
                    }
                    catch (Exception ex)
                    {
                        SqlSugarHelper.Db.Ado.RollbackTran();
                        throw ex;
                    }
                    
                    w.DialogResult = true;
                });
                return command;
            }
        }

        public RelayCommand<Window> CancelCommand
        {
            get
            {
                var command = new RelayCommand<Window>((w) =>
                {
                    

                    w.DialogResult = false;
                });
                return command;
            }
        }
    }
}
